package com.maxlen.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.maxlen.order.entity.vo.PayVO;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000122604908";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC4/r5xALgr95d6EdFfXbHEHC/L8aPKltaAma2XD2vZQHKiv7rUBU8tGl7jVE4dTAcGSzmW697CQQe6kgndXExhBtqqKNvAB1oqMKa9lK0tjRvbfv5Tk0ak1izIFqvNNBYJnZ8xThDXmuxh3w+bPLlo6Oq+a27ar61hHCKZ84iu/8ZwsZ0LKMYjEciTNPWVrv6VRY6Qf2V+WIy+Hw95ik1if7ivleEKSwwvqVJgJqp++nmM7QWZm5RmBwVzEj7If0iQ008CyVlOTPJhTV5AbOXCv5iSEymMEzk6Cta0tyFaBtE3FfwgjLgecADSkIc80G3cxK9OlBcNPFqgW0wN4XWpAgMBAAECggEAKfOrIFk3atEVCturjWLkA0MZG7jKmhCmyDcRhKY8oOHanUm5hodFs0a9qfPeWwp4tVat0qnetFKm/ApOLmLV91Lkl534Z43DRroNtgRXWXMclCQybKWDabIgKafqgfiD3t5d0YKQkzRQ0skQ8vVMXpwjT9dxJiqcuN72gc8WegioBh5ySj/v62RIZlvqKGTP5G8LmmPKBEmHhxjnm3J9x5mHb4JOBi6+UmcOMg2kA3p7QJ5WbdAmji+6r/3qQ9JIQSWHM63qyW2NFv3mdiO9acw4/ZS/LgKLwa6RUnLdjjKc4kic+95qxWtnX+N2vjwDfD60DS0ce/RAyUaBRDzaYQKBgQDnbSN//7q64Fviup3QYkh/Jnhyx2nZhSYaqjWfOPQkgSgFEOvvnxWj/Q4T320/xPpySlR0TZ3e8dVcLkOooa9fjlVMdNPZNje4woQWlTeJ5/3iQc4FIvYv5rygxr4lMwrXqPaCXs8RA8JnF2ZXMuCXpaL73NECghFj4Mq84qS0xQKBgQDMo3cX9+1VuB0Uew1wTAzRnBZQkrKZPEeRG0+svax/LTbIpqkoolmsqkNRFj3GEcVY0cdDm98KrZVPkKf2ne6JAbQgF5GNVZIiAzEWisC3xpwLX+o/0ep3qmy3YE9ROeUDJr8P2S1l/voiphEY0xQ4mdQUUhULFf1flq817g0zlQKBgQDHdF2LPLkPxLwMna/T4FEBHQPkUI+exE3B336Ver2YFKSMMThW7fH1wux2HjMHKdp71CoTjq2Ibz7m0ozHcoo2PfKwEWRgKYU+vI6g8/vI5GQAs5w++xHY2PErU/6fCu3GTr4eQF4QGQBcX2N5tcSxjjT5Ayg0FCuuvBefUWqIzQKBgQCtlqiKu3QnN6PrYKq4rLpfAIHpXVeFJx4qWDmQ9eCijQ8gzEdIQdfB4ThzS2OhZie6vVDD6DAelbrHoJTqXXQf4kMSygtdEc6BbaWxUohCHIb4GyfRdVbopbfNIjSGWY8Lyk1eu0KId+9lfuuX3E4V6V9+0VYqbj/dWyxLmiC2tQKBgQDAgmaRXrV5awYbVfkn38FjPKceG8GEytCSfo4SaqRbpTNYum6Z3XbRTSLeBQRtQqBHIY073ib24HlrPRz7OUCyNYP280cj2GT28aA6KpUxvWavn2lcs7+53awCTTUVSMaZCi+hOHkPp/2sXfF322g5OyS5MsoDTpfPvNxnm25abQ==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAobFSjeNFCdgSDPOYO6kaDAE6D2TAmRl++sHwQmaAM1h6bYzcinEFWERHpCcjLEISt69GLCFMkj/y2rWehw9kyiLOg8uTHCFyIDknZoO3NJ7TGVuUM30SSozJ4rBm99gdqg7KUGveWkRLYZKJMNzy258LNv5DjuEi8DGft6nLiFYH2QLKmqsPB329Ky3WemvmxwDak/K+idIoROrSiz54E+EloGU+ZKoqmD9rVA274LKjs94zA2Oc1uTgwCyyH1lhgMuMioOaGgZDR/698AcMYPL81w9KZxH8PybkWddb9h8BOGzvJsk4KVPgOxIvW8PI6MtaVpSZ65dc2wevbr+mcQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://bkbb24.natappfree.cc/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://localhost:9527/order-tag";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVO vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
